/*
*	Developed by Leonardo Chatain and Pedro Dusso, at Universidade Federal do Rio Grande do Sul, Brasil, 2009/1
*	Source available at glpacman.codeplex.com
*/

#include "Character.h"

Character::Character() {
	position.setXY(0, 0);

	setDiscreteDirection(Character::up);
	turning = false;
	linearSpeed = 1;
	angularSpeed = 5;
	turningDirection = discreteDirection;
}
/*
void Character::turnLeft() {
	setDiscreteDirection((Character::Directions)(((int)discreteDirection + 1)%4));
}

void Character::turnRight() {
	setDiscreteDirection((Character::Directions)(((int)discreteDirection + 3)%4));
}*/

void Character::walk() {
	if (isTurning()) {
		Vector2D tDir;
		switch(turningDirection) {
			case up:
				tDir.setXY(UP);
				break;
			case left:
				tDir.setXY(LEFT);
				break;
			case down:
				tDir.setXY(DOWN);
				break;
			case right:
				tDir.setXY(RIGHT);
				break;
		}
		double theta = direction.getAngleTo(tDir);
		float turnAngle = angularSpeed;
		if (theta > 180) {
			theta = theta - 360;
			turnAngle *= -1;
		}
		// OK, the turning action has ended
		if (fabs(theta) < 2) {
			setDiscreteDirection(turningDirection);
			turning = false;
		}
		else {
			direction.rotate(turnAngle);
		}

	}
	else {
		position.setX(getX() + direction.getX()*linearSpeed);
		position.setY(getY() + direction.getY()*linearSpeed);
	}
}
